const { Transform } = require('stream');
const _ = require('lodash');
const moment = require('moment');
const utils = require('../../utils');

module.exports = function (file) {
  const stream = new Transform({ objectMode: true });
  const func = utils.parseFunction(file);

  stream._transform = async function (chunk, encoding, callback) {
    utils.debug('scriptTransform [chunk]:', chunk);
    const obj = await func(chunk, { _, moment, utils });
    stream.push(obj);
    callback();
  };

  stream.on('finish', function () {
    utils.debug('scriptTransform [finish]');
  });

  return stream;
};